package huawei;

import java.util.*;

/**
 * @Classname Question_3
 * @Author happytaohaha
 * @Date 2020/3/11
 * @Description 栈的应用
 */
public class Main {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String question = sc.nextLine();
        if(question.equals("")) {
            System.out.println("EXPAND:");

            System.out.println("CIRCLE:");
        }else{
            String[] questions = question.split(";");
            Stack<String> stack = new Stack<>();
            List<String> result = new ArrayList<String>();
            List<String> circles = new ArrayList<String>();
            if (questions.length > 0) {
                String[] head = questions[0].split(" ");
                stack.push(head[0]);
                stack.push(head[1]);
                result.add(head[0]);
                result.add(head[1]);
            }
            int len = questions.length;
            for (int i = 1; i < len; i++) {
                String[] origin = questions[i].split(" ");
                if (!stack.peek().equals(origin[0])) {
                    int j = i;
                    while (j < len && !questions[j].split(" ")[0].equals(stack.peek())) {
                        j++;
                    }
                    //如果j 等于len 就说明没有相等的
                    if (j < len) {
                        String temp = questions[i];
                        questions[i] = questions[j];
                        questions[j] = temp;
                        origin = questions[i].split(" ");
                    }
                }
                //如果已包含，就找环加到环里
                if (stack.contains(origin[1])) {
                    List<String> circle = new ArrayList<>();
                    circle.add(origin[1]);
                    while (stack.contains(origin[1])) {
                        circle.add(stack.pop());
                    }
                    List<String> circleres = new ArrayList<String>();
                    int len1 = circle.size();
                    for (int j = 0; j < len1; j++) {
                        circleres.add(circle.get(len1 - j - 1));
                    }
                    circles.add(String.join(" ", circleres));
                }
                //如果未包含就直接加进去顺便加到结果组里
                else {
                    stack.push(origin[1]);
                    result.add(origin[1]);
                }
            }

            String res = String.join(" ", result);
            System.out.println("EXPAND:" + res);

            String cir = String.join(";", circles);
            System.out.println("CIRCLE:" + cir);
        }

    }
}
